<?php
/**************************
 * Created on Jan 22, 2011
 * By: Jos Pape
 * info: jospape@hotmail.com
 */
 
function getNiceDuur($minutes)
{
	$retVal = '';
	$totalDuur = $minutes;
	$jaren = 0;
	$dagen = 0;
	$uren = 0;
	$minuten = 0;
	while($totalDuur > (60 * 24 * 365))
	{ 
		$jaren++;
		$totalDuur -= (60 * 24 * 365);
	}
	while($totalDuur > (60 * 24))
	{ 
		$dagen++;
		$totalDuur -= (60 * 24);
	}
	while($totalDuur > (60))
	{ 
		$uren++;
		$totalDuur -= (60);
	}
	
	if($jaren > 0)
		$retVal .= $jaren.' jaar ';
		
	if($dagen > 1)
		$retVal .= $dagen.' dagen ';
	elseif($dagen > 0)
		$retVal .= $dagen.' dag ';
		
	if($uren > 1)
		$retVal .= $uren.' uren ';
	elseif($uren > 0)
		$retVal .= $uren.' uur ';
		
	if($retVal != '')
		$retVal .= ' en ';
	$retVal .= $totalDuur.' minuten';
	return $retVal;
} 
 
function createCleanURL($url)
{
	$url = str_replace("http://","",$url);
	$url = str_replace("https://","",$url);
	$url = str_replace("ftp://","",$url);
	return $url;
}
 
function getSexName($sexID, $field = 'Short', $language = 1)
{
	global $_MySQL;
	$query = "	SELECT 
					* 
				FROM Sexes 
				WHERE ID = '".str_replace("'","",$sexID)."'
					AND Language = '".str_replace("'","",$language)."'";
	$result = $_MySQL->runQuery($query);
	$row = mysql_fetch_assoc($result);
	if(isset($row[$field]))
		return $row[$field];
	else 	
		return $row['Short'];
	
}

function getNiceDateformat($string, $showtime = false)
{
	$retVal = "Error";
	if(strlen($string) == 10 && preg_match("/\d\d\d\d-\d\d-\d\d/", $string))
	{
		$dag = substr($string, 8, 2)*1;
		$maand = getMonth(substr($string, 5, 2)*1);
		$jaar = substr($string, 0, 4);
		$retVal = $dag." ".$maand." ".$jaar;
	}
	elseif(strlen($string) == 10 && preg_match("/\d\d-\d\d-\d\d\d\d/", $string))
	{
		$dag = substr($string, 0, 2)*1;
		$maand = getMonth(substr($string, 3, 2)*1);
		$jaar = substr($string, 6, 4);
		$retVal = $dag." ".$maand." ".$jaar;
	}
	elseif(strlen($string) == 19 && preg_match("/\d\d-\d\d-\d\d\d\d \d\d:\d\d:\d\d/", $string))
	{
		$dag = substr($string, 0, 2)*1;
		$maand = getMonth(substr($string, 3, 2)*1);
		$jaar = substr($string, 6, 4);
		$retVal = $dag." ".$maand." ".$jaar;
		if($showtime)
		{
			$retVal .= " om ";
			$retVal .= substr($string, 11, 2).":";
			$retVal .= substr($string, 14, 2);
		}
	}
	elseif(strlen($string) == 19 && preg_match("/\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d/", $string))
	{
		$dag = substr($string, 8, 2)*1;
		$maand = getMonth(substr($string, 5, 2)*1);
		$jaar = substr($string, 0, 4);
		$retVal = $dag." ".$maand." ".$jaar;
		if($showtime)
		{
			$retVal .= " om ";
			$retVal .= substr($string, 11, 2).":";
			$retVal .= substr($string, 14, 2);
		}
	}
	return $retVal;
}

function checkEmail($mail) 
{
	$valid = false;
    // explode het e-mail adres op een @
    $email_host = explode("@", $mail); 
    // pak alles na de @
    $email_host = $email_host['1'];
    // zet een var met de (eventuele) DNS van de domein
    $email_resolved = gethostbyname($email_host); 

    // kijk of er een DNS aan zit en of alles voor en na het e-mail adres klopt
    if ($email_resolved != $email_host && eregi("^[0-9a-z]([-_.~]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$",$mail)) 
        $valid = true; 

    // wanneer het geldig is, is $valid = 1, zo niet, dan geeft hij geen result (NULL)
    return $valid; 
} 

function getDateFromWeek($day, $iWeek, $iYear)
{
	$dayArr = array(0=>"Maandag", 1=>"Dinsdag", 2=>"Woensdag", 3=>"Donderdag", 4=>"Vrijdag", 5=>"Zaterdag", 6=>"Zondag");

	 // Get a random monday in the requested year
	$iRandomDate = mktime(0, 0, 0, 3, 1, $iYear);
	 // What's the first monday before this random date?
	$iMonday = mktime(0, 0, 0, date('n', $iRandomDate), date('j', $iRandomDate)-(date('w', $iRandomDate)-1), $iYear);
	 // Get the date of week 1
	$iFirstWeek = mktime(0, 0, 0, date('n', $iMonday), date('j', $iMonday) - 7*(date('W', $iMonday)-1), $iYear);

	$timeStamp = mktime(0, 0, 0, date('n', $iFirstWeek), date('j', $iFirstWeek) + 7*($iWeek-1), date('Y', $iFirstWeek));
	$timeStamp += array_search($day, $dayArr) * (60 * 60 * 24);
	return $timeStamp;
}


function getMonth($monthnum)
{
	switch($monthnum) 
	{
		case 1:
			return "januari";
			break;
		case 2:
			return "februari";
			break;
		case 3:
			return "maart";
			break;
		case 4:
			return "april";
			break;
		case 5:
			return "mei";
			break;
		case 6:
			return "juni";
			break;
		case 7:
			return "juli";
			break;
		case 8:
			return "augustus";
			break;
		case 9:
			return "september";
			break;
		case 10:
			return "oktober";
			break;
		case 11:
			return "november";
			break;
		case 12:
			return "december";
			break;
	}
}

function getPageRights($pageID)
{
	global $_MySQL;
	global $_SESSION;
	$retVal = 0;
	
	if(isset($_SESSION['login']['GroupID']))
	{
		$query = "SELECT Rights FROM UserGroupRights WHERE GroupID = '".str_replace("'","",$_SESSION['login']['GroupID'])."'";
		$result = $_MySQL->runQuery($query);
		$row = mysql_fetch_assoc($result);
		$retVal = $row['Rights'] * 1;
	}
	
	return $retVal;
}
/**
 * Excel creating function
 */
function xlsBOF() {
    echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);  
    return;
}

function xlsEOF() {
    echo pack("ss", 0x0A, 0x00);
    return;
}

function xlsWriteNumber($Row, $Col, $Value) {
    echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
    echo pack("d", $Value);
    return;
}

function xlsWriteLabel($Row, $Col, $Value ) {
    $L = strlen($Value);
    echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
    echo $Value;
	return;
}


function catogorieHasSub($catogorieID)
{
	global $_MySQL;
	$query = "SELECT * FROM ProductCatogorien WHERE CatogorieID = '".str_replace("'","",$catogorieID)."' LIMIT 1";
	$result = $_MySQL->runQuery($query);
	return (mysql_num_rows($result) > 0);
}

function catogorieHasProducts($catogorieID)
{
	global $_MySQL;
	$query = "SELECT * FROM Producten WHERE CatogorieID = '".str_replace("'","",$catogorieID)."' LIMIT 1";
	$result = $_MySQL->runQuery($query);
	return (mysql_num_rows($result) > 0);
}
?>
